package com.apes.fn.controlling.innerAssist;

import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.Arith;
import com.apes.framework.util.MapUtil;
import com.apestech.framework.json.JSONObject;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Creator: zouyc
 * CreateDate: 2021-04-20 9:26
 * Description:
 **/
@Service("innerAssistService")
public class InnerAssistService {

    public static Integer beetweenDay =  12; //查询时间不能大于12个月


    /**
     * 传入时间，不能大于一年
     *
     *入参：
     * 统计时间： 开始时间： DATE_START   截止时间：  DATE_END
     *
     * 对应sqlxml文件:
     */



    /**
     * esb:  fn.innerAssistService.innerEfficiencyBar
     * 内协效率柱形图
     *
     * 字段
     * 时间：time
     * 单量：amount
     * 耗时：consume
     *
     *
     *
     * @param re
     * @return
     */
    public List innerEfficiencyBar(SimpleRequest re){
        List result = (List) new InnerEngine<List,List>() {
            @Override
            protected List doProcess(List t) { //对查询结果的处理
                t.stream().forEach(item->{
                    JSONObject json = (JSONObject)item;
                    try {
                        json.put("consume",json.getDoubleValue("AMOUNT") == 0 ? 0 : Arith.roundUp(json.getDoubleValue("CONSUMETIME")/json.getDoubleValue("AMOUNT"),1));
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }

                });
                return t;
            }

            @Override
            protected List resultMap(List list) { //对返回结构的处理
                return (List) list.stream().map(item->{
                    JSONObject json = (JSONObject)item;
                    return MapUtil.mapper(
                            "time",json.get("MOUTH"),
                                    "amount",json.get("AMOUNT"),
                                    "consume",json.get("consume")
                    );
                }).collect(Collectors.toList());
            }
        }.setCondition("innerAssist.innerEfficiencyBar",new JSONObject(re.getJO()),true,false).process();
        return result;
    }

    /**
     * esb:  fn.innerAssistService.innerEfficiencyGrid
     * 内协效率表格
     *
     * 字段
     * 统计时间： time
     * 基地编码： baseId
     * 基地名称： baseName
     * 内协接车数量： pickAmount
     * 占比：   accountFor
     * 内协接车平均耗时： averageTime
     *
     * @param re
     * @return
     */
    public List innerEfficiencyGrid(SimpleRequest re){
        List result = (List) new InnerEngine<List,List>() {
            @Override
            protected List doProcess(List t) { //对查询结果的处理

                double allSize = t.stream().mapToDouble((item)->{
                    JSONObject itemTemp = (JSONObject)item;
                    try {
                        return itemTemp.getDoubleValue("AMOUNT");
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                }).sum();


                t.stream().forEach(item->{
                    JSONObject json = (JSONObject)item;
                    try {
                        json.put("consume",json.getDoubleValue("AMOUNT") == 0 ? 0 : Arith.roundUp(json.getDoubleValue("CONSUMETIME")/json.getDoubleValue("AMOUNT"),1));
                        json.put("accountFor",allSize == 0 ? 0 : Arith.roundUp(json.getDoubleValue("AMOUNT")/allSize * 100,1) );
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }

                });
                return t;
            }

            @Override
            protected List resultMap(List list) { //对返回结构的处理
                return (List) list.stream().map(item->{
                    JSONObject json = (JSONObject)item;
                    return MapUtil.mapper(
                            "time",json.get("MOUTH"),
                            "baseId",json.get("OTHER_SIDE_DEPT_ID"),
                            "baseName",json.get("OTHER_SIDE_DEPT_NAME"),
                            "pickAmount",json.get("AMOUNT"),
                            "accountFor",json.get("accountFor"),
                            "averageTime",json.get("consume")
                    );
                }).collect(Collectors.toList());
            }
        }.setCondition("innerAssist.innerEfficiencyGrid",new JSONObject(re.getJO()),false,false).process();
        return result;
    }
    /**
     * esb: fn.innerAssistService.innerBasePie
     * 内协基地统计饼图
     *
     * 基地编码：  baseId
     * 基地名称： baseName
     * 百分比： percent
     *
     * @param re
     * @return
     */
    public List innerBasePie(SimpleRequest re){
        List result = (List) new InnerEngine<List,List>() {
            @Override
            protected List doProcess(List t) { //对查询结果的处理

                double allSize = t.stream().mapToDouble((item)->{
                    JSONObject itemTemp = (JSONObject)item;
                    try {
                        return itemTemp.getDoubleValue("AMOUNT");
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }
                }).sum();


                t.stream().forEach(item->{
                    JSONObject json = (JSONObject)item;
                    try {
                        json.put("percent",allSize == 0 ? 0 : Arith.roundUp(json.getDoubleValue("AMOUNT")/allSize * 100,1) );
                    } catch (Exception e) {
                        throw new RuntimeException(e.getMessage());
                    }

                });
                return t;
            }

            @Override
            protected List resultMap(List list) { //对返回结构的处理
                return (List) list.stream().map(item->{
                    JSONObject json = (JSONObject)item;
                    return MapUtil.mapper(
                            "percent",json.get("percent"),
                            "baseId",json.get("OTHER_SIDE_DEPT_ID"),
                            "baseName",json.get("OTHER_SIDE_DEPT_NAME")
                    );
                }).collect(Collectors.toList());
            }
        }.setCondition("innerAssist.innerBasePie",new JSONObject(re.getJO()),false,false).process();
        return result;
    }
    /**
     * esb: fn.innerAssistService.innerBaseGrid
     * 内协基地统计横向柱行图
     *
     * 基地编码： baseId
     * 基地名称： baseName
     * 查询月数量： amount
     * 较上月数量： lastAmount
     * @param re
     * @return
     */
    public List innerBaseGrid(SimpleRequest re){
        List result = (List) new InnerEngine<List,List>() {
            @Override
            protected List doProcess(List t) { //对查询结果的处理
                t.stream().forEach(item ->{
                    try {
                        JSONObject itemJson = (JSONObject)item;
                        itemJson.put("lastAmount",itemJson.getDoubleValue("LASTAMOUNT"));
                    }catch (Exception e){
                        throw new RuntimeException(e.getMessage());
                    }
                });
                return t;
            }

            @Override
            protected List resultMap(List list) { //对返回结构的处理
                return (List) list.stream().map(item->{
                    JSONObject json = (JSONObject)item;
                    return MapUtil.mapper(
                            "baseId",json.get("OTHER_SIDE_DEPT_ID"),
                            "baseName",json.get("OTHER_SIDE_DEPT_NAME"),
                            "amount",json.get("AMOUNT"),
                            "lastAmount",json.get("lastAmount")
                    );
                }).collect(Collectors.toList());
            }

            @Override
            protected  void dealStateDate(JSONObject jsonRe, JSONObject cjsonRe){ //扔进上个月数据
                try {
                    if(Objects.equals(jsonRe.getString("OTHER_SIDE_DEPT_ID"),cjsonRe.getString("OTHER_SIDE_DEPT_ID"))) jsonRe.put("LASTAMOUNT",cjsonRe.getDoubleValue("AMOUNT")); //添加上月数据
                }catch (Exception e){
                    throw new RuntimeException(e.getMessage());
                }

            };
        }.setCondition("innerAssist.innerBaseGrid",new JSONObject(re.getJO()),false,true).process();
        return result;
    }

}